<!--
 * @Descripttion:  基本设置
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-12-23 16:51:31
-->
<template>
  <div class="lb-app-sweep-set">
    <top-nav></top-nav>
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="setForm"
        ref="setForm"
        :rules="setFormRules"
        label-width="140px"
      >
        <el-form-item label="页面标题" prop="title">
          <el-input
            v-model="setForm.title"
            placeholder="请输入支付页面的标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="公司名称" prop="company_name">
          <el-input
            v-model="setForm.company_name"
            placeholder="请输入公司名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="公司logo" prop="company_logo">
          <lb-cover
            :fileList="setForm.company_logo"
            @selectedFiles="getCover"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸：120 * 120</lb-tool-tips>
        </el-form-item>
        <el-form-item label="满" prop="first_full">
          <el-input-number
            class="lb-input-number"
            v-model="setForm.first_full"
            :controls="false"
            :precision="2"
            :min="0"
          ></el-input-number>
          <lb-tool-tips
            >用户在扫码支付页面授权手机号且支付金额达到这里设置的金额就可以享受优惠</lb-tool-tips
          >
        </el-form-item>
        <el-form-item label="减" prop="first_reduce">
          <el-input-number
            class="lb-input-number"
            v-model="setForm.first_reduce"
            :controls="false"
            :precision="2"
            :min="0"
          ></el-input-number>
          <lb-tool-tips
            >用户在扫码支付页面授权手机号且支付金额达到这里设置的金额就可以享受优惠</lb-tool-tips
          >
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo('setForm')">{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    let valiDateFloat = (rule, value, callback) => {
      let reg = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
      if (value === 0 || reg.test(value)) {
        callback()
      } else {
        callback(new Error('请输入金额，最多两位小数'))
      }
    }
    return {
      setForm: {
        title: '',
        company_name: '',
        company_logo: '',
        first_full: '',
        first_reduce: ''
      },
      setFormRules: {
        title: { required: true, type: 'string', message: '请输入支付页面的标题', trigger: 'blur' },
        // company_name: {required: true, type: 'string', message: '请输入公司名称', trigger: 'blur'},
        company_logo: { required: true, type: 'array', message: '请上传公司的LOGO', trigger: 'change' },
        first_full: { required: true, validator: valiDateFloat, trigger: 'blur' },
        first_reduce: { required: true, validator: valiDateFloat, trigger: 'blur' }
      }
    }
  },
  created () {
    this.getFormInfo()
  },
  methods: {
    getFormInfo () {
      this.$api.getSweepPaymentSettingInfo().then(res => {
        console.log(res)
        if (res.code === 200) {
          this.setForm = res.data
          this.setForm.company_logo = [{ url: res.data.company_logo }]
        }
      })
    },
    getCover (img) {
      this.setForm.company_logo = img
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let subForm = JSON.parse(JSON.stringify(this.setForm))
          subForm.company_logo = subForm.company_logo.map(item => { return item.url })
          if (subForm.first_full * 1 <= subForm.first_reduce * 1) {
            this.$message.error("'满'额度必须大于'减'额度")
          } else {
            this.$api.sweepPaymentSwtting(subForm).then(res => {
              console.log(res)
              if (res.code === 200) {
                this.$message.success(this.$t('tips.successSub'))
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-app-sweep-set {
  width: 100%;
  .page-main {
    .el-form {
      .el-input,
      .el-input-number {
        width: 300px;
      }
    }
  }
}
</style>
